package cn.javasea.atomikos.user;

import cn.javasea.atomikos.user.ds1.UserInfoDs1Dao;
import cn.javasea.atomikos.user.ds2.UserInfoDs2Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

	@Autowired
	UserInfoDs1Dao userInfoDs1Dao;

	@Autowired
	UserInfoDs2Dao userInfoDs2Dao;

	public List<User> getUserInfoByUserName(String userName) {
		List<User> userInfoDs1 = userInfoDs1Dao.findByUserName(userName);
		List<User> userInfoDs2 = userInfoDs2Dao.findByUserName(userName);

		List<User> list = new ArrayList<>();
		list.addAll(userInfoDs1);
		list.addAll(userInfoDs2);
		return list;
	}

	@Transactional(rollbackFor = Exception.class)
	public List<User> insert(User user) {
		userInfoDs1Dao.insert(user);
		userInfoDs2Dao.insert(user);
//		int i = 1 / 0;
		return getUserInfoByUserName(user.getName());
	}

}
